import java.util.PriorityQueue;

//优先级队列_最后一块石头重量（模拟题，简单）
//https://leetcode.cn/problems/last-stone-weight/description/
public class Test {
    public static void main(String[] args) {
        //
        Solution s = new Solution();
        int[] stones = {2,7,4,1,8,1};
        System.out.println(s.lastStoneWeight(stones));
    }
}

class Solution {
    public int lastStoneWeight(int[] stones) {
        //1.创建一个大根堆
        PriorityQueue<Integer> heap = new PriorityQueue<Integer>((a,b) -> b-a);
        if (stones.length == 1) return stones[0];
        for(int stone : stones){
            heap.offer(stone);
        }

        while(heap.size() >= 2){
            int a = heap.poll();
            int b = heap.poll();
            if(a != b){
                heap.offer(a-b);
            }
        }
        if(heap.size() == 0){
            return 0;
        }
        return heap.peek();

    }
}
